home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_cutpaste_win32.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-12  |  4.1 KB  |  143 lines

  1. //
  2. // "$Id: Fl_cutpaste_win32.cxx,v 1.5.2.3 1999/06/12 15:09:19 mike Exp $"
  3. //
  4. // WIN32 cut/paste for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Implementation of cut and paste.
  27.  
  28. // This is seperated from Fl.C mostly to test Fl::add_handler().
  29. // But this will save a small amount of code size in a program that
  30. // has no text editing fields or other things that call cut or paste.
  31.  
  32. #include <FL/Fl.H>
  33. #include <FL/x.H>
  34. #include <FL/Fl_Widget.H>
  35. #include <string.h>
  36. #include <stdio.h>
  37.  
  38. static char *selection_buffer;
  39. static int selection_length;
  40. static int selection_buffer_length;
  41. static char beenhere;
  42.  
  43. extern Fl_Widget *fl_selection_requestor; // widget doing request_paste()
  44.  
  45. static int selection_xevent_handler(int) {
  46.  
  47.   switch (fl_msg.message) {
  48.  
  49.   case WM_DESTROYCLIPBOARD: {
  50.       Fl_Window *w = Fl::first_window();
  51.       while (w != (Fl_Window *)0)
  52.         if (fl_msg.hwnd == fl_xid(w)) break;
  53.         else w = Fl::next_window(w);
  54.  
  55.       if (w == (Fl_Window *)0) {
  56.         Fl::selection_owner(0);
  57.         Fl::flush(); // get the redraw to happen
  58.       }
  59.       return 1;
  60.     }
  61.  
  62.   case WM_RENDERALLFORMATS:
  63.     if (!OpenClipboard(fl_xid(Fl::first_window()))) return 0;
  64.     EmptyClipboard();
  65.     // fall through...
  66.   case WM_RENDERFORMAT: {
  67.     HANDLE h = GlobalAlloc(GHND, selection_length+1);
  68.     if (h) {
  69.       LPSTR p = (LPSTR)GlobalLock(h);
  70.       memcpy(p, selection_buffer, selection_length);
  71.       p[selection_length] = 0;
  72.       GlobalUnlock(h);
  73.       SetClipboardData(CF_TEXT, h);
  74.     }
  75.     if (fl_msg.message == WM_RENDERALLFORMATS)
  76.       CloseClipboard();
  77.     return 1;}
  78.  
  79.   default:
  80.     return 0;
  81.   }
  82. }
  83.  
  84. ////////////////////////////////////////////////////////////////
  85.  
  86. // call this when you create a selection:
  87. void Fl::selection(Fl_Widget &owner, const char *stuff, int len) {
  88.   if (!stuff || len<0) return;
  89.   if (len+1 > selection_buffer_length) {
  90.     delete[] selection_buffer;
  91.     selection_buffer = new char[len+100];
  92.     selection_buffer_length = len+100;
  93.   }
  94.   memcpy(selection_buffer, stuff, len);
  95.   selection_buffer[len] = 0; // needed for direct paste
  96.   selection_length = len;
  97.   if (OpenClipboard(fl_xid(Fl::first_window()))) {
  98.     EmptyClipboard();
  99.     SetClipboardData(CF_TEXT, NULL);
  100.     CloseClipboard();
  101.     if (!beenhere) {
  102.       Fl::add_handler(selection_xevent_handler);
  103.       beenhere = 1;
  104.     }
  105.   }
  106.   selection_owner(&owner);
  107. }
  108.  
  109. ////////////////////////////////////////////////////////////////
  110.  
  111. // Call this when a "paste" operation happens:
  112. void Fl::paste(Fl_Widget &receiver) {
  113.   if (selection_owner()) {
  114.     // We already have it, do it quickly without window server.
  115.     // Notice that the text is clobbered if set_selection is
  116.     // called in response to FL_PASTE!
  117.     Fl::e_text = selection_buffer;
  118.     Fl::e_length = selection_length;
  119.     receiver.handle(FL_PASTE);
  120.   } else {
  121.     if (!OpenClipboard(fl_xid(Fl::first_window()))) return;
  122.     HANDLE h = GetClipboardData(CF_TEXT);
  123.     if (h) {
  124.       Fl::e_text = (LPSTR)GlobalLock(h);
  125.       LPSTR a,b;
  126.       a = b = Fl::e_text;
  127.       while (*a) { // strip the CRLF pairs ($%$#@^)
  128.     if (*a == '\r' && a[1] == '\n') a++;
  129.     else *b++ = *a++;
  130.       }
  131.       *b = 0;
  132.       Fl::e_length = b - Fl::e_text;
  133.       receiver.handle(FL_PASTE);
  134.       GlobalUnlock(h);
  135.     }
  136.     CloseClipboard();
  137.   }
  138. }
  139.  
  140. //
  141. // End of "$Id: Fl_cutpaste_win32.cxx,v 1.5.2.3 1999/06/12 15:09:19 mike Exp $".
  142. //
  143.